﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: TukeysBiweight.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;

namespace BOL.Linq.Descriptive
{
    public static partial class Descriptive
    {
        public static decimal TukeysBiweight(this IEnumerable<decimal> source, double c)
        {
            return (decimal)source.Select(x => (double)x).TukeysBiweight(c);
        }

        /// <summary>
        /// http://conservancy.umn.edu/bitstream/117002/1/v18n4p301.pdf
        /// </summary>
        /// <param name="source"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static double TukeysBiweight(this IEnumerable<double> source, double c)
        {
            var list = source.ToList();
            var count = list.Count();
            var sortedList = list.OrderBy(x => x).ToList();
            var q1 = count / 4;
            var m = count / 2;
            var xs = count % 2 == 0 ? (sortedList.ElementAt(m) + sortedList.ElementAt(m - 1)) / 2 : sortedList.ElementAt(m);
            var s = sortedList[q1 + m] - sortedList[q1];
            var z = list.Select(x => (x - xs) / s / c).ToList();
            var w = z.Select(x => Math.Abs(x) <= 1 ? Math.Pow(1 - x * x, 2) : 0).ToList();
            
            return list.Zip(w, (xc, wc) => xc * wc).Sum() / w.Sum();
        }

        public static float TukeysBiweight(this IEnumerable<float> source, double c)
        {
            return (float)source.Select(x => (double)x).TukeysBiweight(c);
        }

        public static double TukeysBiweight(this IEnumerable<int> source, double c)
        {
            return source.Select(x => (double)x).TukeysBiweight(c);
        }

        public static double TukeysBiweight(this IEnumerable<long> source, double c)
        {
            return source.Select(x => (double)x).TukeysBiweight(c);
        }

        public static decimal TukeysBiweight<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector, double c)
        {
            return source.Select(selector).TukeysBiweight(c);
        }

        public static double TukeysBiweight<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector, double c)
        {
            return source.Select(selector).TukeysBiweight(c);
        }

        public static float TukeysBiweight<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector, double c)
        {
            return source.Select(selector).TukeysBiweight(c);
        }

        public static double TukeysBiweight<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector, double c)
        {
            return source.Select(selector).TukeysBiweight(c);
        }

        public static double TukeysBiweight<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector, double c)
        {
            return source.Select(selector).TukeysBiweight(c);
        }
    }
}
